package com.shu.juc;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/7/31 9:23
 */

public class AtomicXUpdaterTest implements Runnable {
    static Score score1;
    static Score score2;

    public static AtomicIntegerFieldUpdater<Score> updater = AtomicIntegerFieldUpdater.newUpdater(Score.class,"score1");

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            score1.score1 ++;
            updater.getAndIncrement(score2);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        score1 = new Score();
        score2 = new Score();
        AtomicXUpdaterTest r= new AtomicXUpdaterTest();

        Thread t1=new Thread(r);
        Thread t2=new Thread(r);

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(score1.score1);
        System.out.println(score2.score1);
    }


    public static class Score{
        volatile int score1;
        volatile Long score2;
        volatile String name;
    }
}
